//
//  HSYBaseCollectionController.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseCollectionController.h"

@implementation HSYBaseCollectionController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}

#pragma mark - Create CollectionView

- (void)createCollection {
    
    [self createCollectionViewInParentView:self.view
                                      rect:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-[self getTopBarHeight])
                              sectionInset:UIEdgeInsetsMake(0, 0, 0, 0)
                        minimumLineSpacing:0
                   minimumInteritemSpacing:0
                                  itemSize:CGSizeMake(IPHONE_WIDTH, 44)
                       headerReferenceSize:CGSizeMake(0, 0)
                       footerReferenceSize:CGSizeMake(0, 0)];
}

- (void)createCollectionViewInParentView:(UIView *)parentView rect:(CGRect)rect sectionInset:(UIEdgeInsets)sectionInset minimumLineSpacing:(CGFloat)minimumLineSpacing minimumInteritemSpacing:(CGFloat)minimumInteritemSpacing itemSize:(CGSize)itemSize headerReferenceSize:(CGSize)headerReferenceSize footerReferenceSize:(CGSize)footerReferenceSize {
    
    if (!parentView) {
        return;
    }
    
    UICollectionViewFlowLayout *flowLayout = [self createFlowLayoutWithSectionInset:sectionInset
                                                                 minimumLineSpacing:minimumLineSpacing
                                                            minimumInteritemSpacing:minimumInteritemSpacing
                                                                           itemSize:itemSize
                                                                headerReferenceSize:headerReferenceSize
                                                                footerReferenceSize:footerReferenceSize];
    
    self.collectionView = [self createCollectionViewInParentView:parentView withFlowlayout:flowLayout rect:rect];
    [self.view addSubview:self.collectionView];
}

- (UICollectionViewFlowLayout *)createFlowLayoutWithSectionInset:(UIEdgeInsets)sectionInset minimumLineSpacing:(CGFloat)minimumLineSpacing minimumInteritemSpacing:(CGFloat)minimumInteritemSpacing itemSize:(CGSize)itemSize headerReferenceSize:(CGSize)headerReferenceSize footerReferenceSize:(CGSize)footerReferenceSize {
    
    
    UICollectionViewFlowLayout *flowLayout = [NSObject createFlowLayoutForSectionInset:sectionInset
                                                                withMinimumLineSpacing:minimumLineSpacing
                                                               minimumInteritemSpacing:minimumInteritemSpacing
                                                                              itemSize:itemSize
                                                                andHeaderReferenceSize:headerReferenceSize
                                                                   footerReferenceSize:footerReferenceSize
                                                                       scrollDirection:UICollectionViewScrollDirectionVertical];
    return flowLayout;
}

- (UICollectionView *)createCollectionViewInParentView:(UIView *)parentView withFlowlayout:(UICollectionViewFlowLayout *)flowLayout rect:(CGRect)rect {
    
    if (!flowLayout) {
        return nil;
    }
    
    
    UICollectionView *collectionView = [NSObject createCollectionViewForFrame:rect
                                                     withCollectionViewLayout:flowLayout
                                                                   dataSource:self
                                                                     delegate:self
                                                             canScrollEnabled:YES
                                                 showsVerticalScrollIndicator:NO
                                               showsHorizontalScrollIndicator:NO
                                                                      bounces:YES
                                                         alwaysBounceVertical:YES];
    
    return collectionView;
}



#pragma mark - UICollectionViewDelegate && UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    UICollectionViewCell *cell = [self getCollectionViewCellForIndexPath:indexPath withCollectionView:collectionView];
    if (self.selectIndexPathCell) {
        self.selectIndexPathCell(cell, indexPath);
    }
}


#pragma mark - Collection Operation

- (NSIndexPath *)getCollectionViewIndexPathForCell:(UICollectionViewCell *)cell withCollectionView:(UICollectionView *)collectionView {
    
    if (!cell || !collectionView) {
        return nil;
    }
    
    NSIndexPath *indexPath = [collectionView indexPathForCell:cell];
    return indexPath;
}


- (UICollectionViewCell *)getCollectionViewCellForIndexPath:(NSIndexPath *)indexPath withCollectionView:(UICollectionView *)collectionView {
    
    if (!indexPath || !collectionView) {
        return nil;
    }
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    return cell;
}

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withCollectionView:(UICollectionView *)collectionView {
    
    if (!indexPaths || !collectionView) {
        return;
    }
    [collectionView deleteItemsAtIndexPaths:indexPaths];
}


#pragma mark - Pull Down && Up


- (void)addPullDown {
    
    __weak HSYBaseCollectionController *vc = self;
    [self addPullDownToScrollView:self.collectionView withActionHandler:^{
        [vc pullDown];
    }];
}

- (void)pullDown {
    
    [self startPullDownAnimationToScrollView:self.collectionView];
    
}

- (void)stopPullDown {
    
    @weakify(self);
    [self stopPullDownAnimationToBlock:^{
        @strongify(self);
        [self stopPullDownAnimationToScrollView:self.collectionView];
    }];
}



- (void)addPullUp {
    __weak HSYBaseCollectionController *vc = self;
    [self addPullUpToScrollView:self.collectionView withActionHandler:^{
        [vc pullUp];
    }];
}

- (void)pullUp {
    
    [self startPullUpAnimationToScrollView:self.collectionView];
    
}

- (void)stopPullUp {
    
    @weakify(self);
    [self stopPullUpAnimationToBlock:^{
        @strongify(self);
        [self stopPullUpAnimationToScrollView:self.collectionView];
    }];
}




@end
